package com.anita.member.domain.model.member;

import com.anita.member.common.exception.BusinessException;

import java.util.regex.Pattern;

/**
 * 手机号值对象
 * @author qiu
 * @date 2025/10/17
 * @description
 */
public record Phone (String value){

    private static final Pattern PATTERN = Pattern.compile("^1[3-9]\\d{9}$");

    public Phone {
        if (value == null || value.isBlank()) {
            throw new BusinessException("会员手机号不能为空");
        }
        // 去掉常见分隔符
        String cleaned = value.replaceAll("[\\s\\-]", "");
        if (!PATTERN.matcher(cleaned).matches()) {
            throw new BusinessException("会员手机号格式不正确");
        }
    }

    public static Phone of(String value) {
        return new Phone(value);
    }

    /**
     * 脱敏显示：前三后四，中间 ****
     */
    public String mask() {
        return value.substring(0, 3) + "****" + value.substring(7);
    }

    public String getValue() {
        return value;
    }
}
